package cn.wolfcode.p2p.business.service.impl;

import cn.wolfcode.p2p.business.domain.BidRequest;
import cn.wolfcode.p2p.business.domain.SystemAccount;
import cn.wolfcode.p2p.business.domain.SystemAccountFlow;
import cn.wolfcode.p2p.business.mapper.SystemAccountFlowMapper;
import cn.wolfcode.p2p.business.service.ISystemAccountFlowService;
import cn.wolfcode.p2p.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

@Service
@Transactional
public class SystemAccountFlowServiceImpl implements ISystemAccountFlowService {
    @Autowired
    private SystemAccountFlowMapper systemAccountFlowMapper;

    private void createBaseFlow(int actionType, BigDecimal amount, SystemAccount systemAccount, String note) {
        SystemAccountFlow flow = new SystemAccountFlow();
        flow.setActionType(actionType);
        flow.setAmount(amount);
        flow.setActionTime(new Date());
        flow.setUsableAmount(systemAccount.getUsableAmount());
        flow.setFreezedAmount(systemAccount.getFreezedAmount());
        flow.setNote(note);
        systemAccountFlowMapper.insert(flow);
    }

    @Override
    public void createBorrowManagementChargeFlow(SystemAccount systemAccount, BigDecimal amount, BidRequest br) {
        createBaseFlow(Constants.SYSTEM_ACCOUNT_ACTIONTYPE_MANAGE_CHARGE, amount, systemAccount,
                "收取[" + br.getTitle() + "]借款管理费:" + amount);
    }


    public void createInterestManagerChargeFlow(SystemAccount systemAccount, BigDecimal amount) {
        createBaseFlow(Constants.SYSTEM_ACCOUNT_ACTIONTYPE_INTREST_MANAGE_CHARGE, amount, systemAccount,
                "收取利息管理费:" + amount);
    }
}
